﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcWebRole1.Models;

namespace MvcWebRole1.Repositories
{
    public class WorkflowRepository
    {
        internal List<Expenses> GetExpenses()
        {
            List<Expenses> result = null;
            using (var dbContext = new WorkflowInstanceStoreDataContext())
            {
                var expenses = from p in dbContext.InstancePromotedProperties
                               select new Expenses()
                               {
                                   DeviceUniqueId = (string)p.Value1,
                                   Amount = (decimal)(p.Value2 ?? 0)
                               };

                result = expenses.ToList(); ;
            }

            return result;
        }

        internal string GetActiveBookmarks(string deviceUniqueId)
        {
            var result = "";
            using (var dbContext = new WorkflowInstanceStoreDataContext())
            {
                var properties = dbContext.InstancePromotedProperties.SingleOrDefault(p => deviceUniqueId == (string)p.Value1);
                if (properties != null)
                {
                    var id = properties.InstanceId;
                    var instance = dbContext.Instances.First(ent => ent.InstanceId == id);
                    result = instance.ActiveBookmarks;
                }
            }

            return result;
        }
    }
}